package xyz.jangle.string;

public class StringMain8 {
	
	public static void main(String[] args) {
		ObjectA a = new ObjectA();
		a.setA("123");
		a.setB("456");
		try {
			ObjectA clone = (ObjectA) a.clone();
			System.out.println(a.getA()==clone.getA());	//true
			a.setA("123456");
			//由于String的不可变性，每次对String的修改都会创建一个新的对象。
			System.out.println(clone.getA());
			a.setA(new String("123"));	// 此处会新开辟一个内存空间
			System.out.println(a.getA()==clone.getA());	//false
			a.setA("123");	//此处会引用 “123”字符串
			System.out.println(a.getA()==clone.getA());	//true
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
	
}

class ObjectA implements Cloneable{
	String a,b;

	public String getA() {
		return a;
	}

	public void setA(String a) {
		this.a = a;
	}

	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
	
	
	
}